# -*- coding: utf-8 -*- 
# @Time : 2020-12-02 22:43 
# @Author : derek.zhang 
# @File : fake_deploy.py 
# @Software: PyCharm
import string
import time
import random

from django.contrib.auth.models import User

from appinput.models import App
from deploy.models import DeployPool, DeployStatus
from envx.models import Env


def fake_deploy_data():
    DeployPool.objects.all().delete()
    print('delete all deploy data')
    app_set = App.objects.all()
    env_set = Env.objects.all()
    user_set = User.objects.all()
    is_inc_tot = ['TOT','INC']
    deploy_type = ['deployall', 'deploykg', 'deploycfg']
    deploy_status_set_env = DeployStatus.objects.filter(name__in=['READY', 'ING', 'FINISH', 'ERROR'])
    deploy_status_set_create = DeployStatus.objects.filter(name = 'CREATE')
    deploy_status_set_build = DeployStatus.objects.filter(name= 'BUILD')

    for data_no in range(30):
        random_letter = ''.join(random.sample(string.ascii_letters, 2))
        time_str = time.strftime('%Y - %m -%d -%H%M%S',time.localtime())
        fake_time_str = time_str.split('-')
        fake_time_str[2] = str(data_no)
        fake_time_str = '-'.join(fake_time_str)
        name = fake_time_str + random_letter.upper()
        DeployPool.objects.create(name=name, description='test',
                                  branch_build = 'master',
                                  jenkins_number = data_no+10,
                                  code_number = data_no +10,
                                  is_inc_tot = random.choice(is_inc_tot),
                                  deploy_type = random.choice(deploy_type),
                                  create_user = random.choice(user_set),
                                  app_name = random.choice(app_set),
                                  env_name = random.choice(env_set),
                                  deploy_status = random.choice(deploy_status_set_env),
                                  nginx_url='http://localhost/'
                                  )
    for data_no in range(30):
        random_letter = ''.join(random.sample(string.ascii_letters,2))
        time_str = time.strftime('%Y - %m -%d -%H%M%S',time.localtime())
        fake_time_str = time_str.split('-')
        fake_time_str[2] = str(data_no)
        fake_time_str = '-'.join(fake_time_str)
        name = fake_time_str + random_letter.upper()
        if data_no % 2 ==1:
            DeployPool.objects.create(name=name, description='test',
                                      branch_build = 'master',
                                      jenkins_number = data_no,
                                      code_number = data_no +10,
                                      is_inc_tot = random.choice(is_inc_tot),
                                      deploy_type = random.choice(deploy_type),
                                      create_user = random.choice(user_set),
                                      app_name = random.choice(app_set),
                                      env_name = random.choice(env_set),
                                      deploy_status = random.choice(deploy_status_set_build),
                                      nginx_url='http://localhost/'
                                      )
        else:
            DeployPool.objects.create(name=name, description='test',
                                      branch_build='master',
                                      jenkins_number=data_no,
                                      code_number=data_no + 10,
                                      is_inc_tot=random.choice(is_inc_tot),
                                      deploy_type=random.choice(deploy_type),
                                      create_user=random.choice(user_set),
                                      app_name=random.choice(app_set),
                                      env_name=random.choice(env_set),
                                      deploy_status=random.choice(deploy_status_set_create),
                                      nginx_url='http://localhost/'
                                      )

    print('create all deploy')

if __name__ == '__main__':
    fake_deploy_data()